package com.huoyun.base.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.huoyun.user.bean.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 分页展示页面每页展示的记录数
	 */
	public static final int PAGE_COUNT = 10;
	
	/**
	 * 当前页码
	 */
    public long pageNo = 1;
	
	/**
	 * 操作结果提示信息
	 */
	public String resultMsg;

	public User loginUser;


	/**
	 * 中获取当前登录的用户
	 * 
	 * @return 用户对象
	 */
	public User getLoginUser() {
		User user = null;

		// 从上下文
		final Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();

		final Object principal = auth.getPrincipal();

		if (principal instanceof User) {
			user = (User) principal;
		}

		return user;
	}

	/**
	 * 获得当前session对象
	 * 
	 * @return session
	 */
	public HttpSession getSession() {
		final HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);

		return request.getSession();
	}

	/**
	 * 获得当前request对象
	 * 
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return (HttpServletRequest) ActionContext.getContext().get(
				ServletActionContext.HTTP_REQUEST);
	}

	/**
	 * 获得当前respone对象
	 * 
	 * @return
	 */
	public HttpServletResponse getResponse() {
		return (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
	}

	/**
	 * 获得application对象
	 * 
	 * @return
	 */
	public Map<String, Object> getApplication() {
		return (Map<String, Object>) ActionContext.getContext().get(
				ServletActionContext.APPLICATION);
	}

	public String getResultMsg() {
		return resultMsg;
	}

	public void setResultMsg(final String resultMsg) {
		this.resultMsg = resultMsg;
	}

	public long getPageNo() {
		if(pageNo < 1)
		{
			pageNo = 1;
		}
		
		return pageNo;
	}

	public void setPageNo(long pageNo) {
		this.pageNo = pageNo;
	}
	
	
}
